package com.gitee.wsl.ext.array


/**
 * Match given [bytes] to this byte array from index [fromOffset]
 * It will match in reverse order since that usage is faster in sorted lists
 */
fun ByteArray.match(fromOffset: Int, bytes: ByteArray, fromLength: Int = this.size, offset: Int = 0, length: Int = bytes.size): Boolean {
    if (length != fromLength) return false
    return (length - 1 downTo 0).all { this[it + fromOffset] == bytes[it + offset] }
}

/**
 * Match given [bytes] to a part from index [fromOffset]
 * It will match in reverse order since that usage is faster in sorted lists
 */
fun ByteArray.matchPart(fromOffset: Int, bytes: ByteArray, fromLength: Int = this.size, offset: Int = 0, length: Int = bytes.size): Boolean {
    if (length > fromLength) return false
    return (length - 1 downTo 0).all { this[it + fromOffset] == bytes[it + offset] }
}

fun ByteArray.startsWith(prefix: ByteArray) = matchPart(0,prefix)